﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace AcademicAudit_JoelShuman
{
	public static class DefaultCourseList
	{
		public static List<Course> DEFAULTCOURSELIST { get; set; }
	}
	public static class CSVParser
	{
		public static List<Course> ParseCourseCSV( String fileName )
		{
			//Read a comma delimited string from a file   
			StreamReader sReader = null;
			FileStream fs = null;
			List<Course> listOfCourses = new List<Course>();

			try
			{
				fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
				sReader = new StreamReader(fs);
				String line;
				char[] trimChars = { '\n', 'r', ' ' };
				CourseType currentType = CourseType.CORE;
				while ((line = sReader.ReadLine()) != null)  //Read comma delimited string from file
				{
					Course tempCourse = new Course();
					// If the line has no commas, and then it is a new CourseType.
					if( ! line.Contains( ",") )
					{

						switch( line.TrimEnd( trimChars))
						{
							case "Core":
								currentType = CourseType.CORE;
								break;
							case "GenEd":
								currentType = CourseType.GENED;
								break;
							case "Electives":
								currentType = CourseType.ELECTIVE;
								break;
							default:
								currentType = CourseType.ELECTIVE;
								break;
						}
						continue;
					}

					//Parse and display each value in the comma delimited string     
					String[] resultArray = line.Split(',');

					// If the array size is 2, get the total credits.
					if( resultArray.Length == 2 )
					{
						continue;
					}

					// If 3, New Course, then Course#, Course Name, Credits
					if( resultArray.Length == 3 )
					{
						tempCourse.m_CourseNumber = resultArray[0];
						tempCourse.m_CourseName = resultArray[1];
						tempCourse.m_Credits = Convert.ToInt32(resultArray[2]);

						// Generate unique courseID; 
						FileStream tempFs = new FileStream("../../DataFiles/CourseID.txt", FileMode.Open, FileAccess.Read);
						StreamReader tempReader = new StreamReader(tempFs);
						String tempLine = tempReader.ReadLine().TrimEnd(trimChars);
						tempReader.Close();
						tempFs.Close();

						int nextIdAvailable = Convert.ToInt32(tempLine);
						tempCourse.m_CourseID = nextIdAvailable;

						tempFs = new FileStream("../../DataFiles/CourseID.txt", FileMode.Truncate, FileAccess.Write);
						StreamWriter tempWriter = new StreamWriter(tempFs);
						tempWriter.WriteLine(++nextIdAvailable);
						tempWriter.Close();
						tempFs.Close();

						// Set default value for Semester, year, grade, and status.
						tempCourse.m_Semester = Semester.NOTSET;
						tempCourse.m_year = 0;
						tempCourse.m_CourseGrade = Grade.UNKNOWN;
						tempCourse.m_CourseStatus = Status.I;

						// Set the course type
						tempCourse.m_CourseType = currentType;

						listOfCourses.Add(tempCourse);
					}

				}
				sReader.Close();

				return listOfCourses;
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
				return null;
			}
			finally
			{
				if (fs != null) fs.Close();
				if (sReader != null) sReader.Close();
			}
		}
	}
}
